<?php
/**
 * $Id: hooks.php 1096 2012-08-16 06:13:10Z shrey $
 * 
 * This file can not be redistributed to anyone without the permission of Giga Promoters
 * Copyright 2005 - 2008, Giga Promoters
 * 						  608 Aggarwal Cyber Plaza
 *						  Netaji Subhash Place, Delhi - 110034
 *						  www.gigapromoters.com
 * 
 *
 * This is parent of parent class of a controller and should not be edited ever without permission.
 * @version 			$Rev: 1096 $
 * @lastModifiedBy		$LastChangedBy: shrey $
 * @lastChanged			$Date: 2012-08-16 11:43:10 +0530 (Thu, 16 Aug 2012) $
 * @copyright 			Giga Promoters (www.gigapromoters.com)
 * 
 */

/**
 * Before render hook can be used to include JS files on the fly.
 * In future JS can be appended by checking isInstalled('someplugin') 
 *
 * @param unknown_type $controller
 */
function CmsbeforeRenderhook($controller)
{

	
}
function CmsInitHook(Controller $ctrl)
{
	//check for maintaining
	if(!defined("CAKEPHP_SHELL") && cr("cmsConfig.Backend.siteUrl") && !isset($_GET['_r']) && !$ctrl->data)
	{
		$http = "http";
		if(env("HTTPS"))
		{
			$http = "https";
		}
		if(!isset($ctrl->params['url']))
		{
			return;
		}
		$uriParts = $ctrl->params['url'];
		$uri = $uriParts['url'];
		if($uri == "/")
		{
			$uri = '';
		}
		unset($uriParts['url']);
		$host = env("HTTP_HOST");
		$mainUrlParts = parse_url(cr("cmsConfig.Backend.siteUrl"));
		$siteHost = $mainUrlParts['host'];
		$get = $uriParts;
		//stands www.check to prevent infinte redirects.
		$get['_r'] = 1;
		$wwwRegex = '/^www\./';
		$stripWWW = false;
		$redirect  = false;
		switch (true)
		{
			//siteHost have www but requested url host doesn't
			case preg_match($wwwRegex,$siteHost) && !preg_match($wwwRegex, $host):
				$redirect = true;
				$stripWWW = false;
				break;
			//siteHost doesn't have www	 but requested url have www
			case !preg_match($wwwRegex,$siteHost) && preg_match($wwwRegex, $host):
				$redirect = true;
				$stripWWW = true;
				break;
				
		}		
		if($redirect)
		{
			if(isset($mainUrlParts['path']))
			{
				$uri = $mainUrlParts['path'].'/'.$uri;
			}
			if($stripWWW)
			{
				$siteHost = preg_replace($wwwRegex,"",$host);
			}else
			{
				$siteHost = "www.".$host;
			}
			unset($get['ext']);
			$getString = http_build_query($get,'','&');
			$redirectonUrl = $http.'://'.$siteHost.'/'.$uri.'?'.$getString;
			$ctrl->redirect($redirectonUrl);
		}
	}
}
function CmsAfterComponentInitHook($controller)
{
	if($controller->Session->check('tester_id'))
	{
		$webtest = $controller->Session->read('tester_id');
		Configure::write('webtest_id', $webtest);
	}
}

function CmsbeforeFilterhook($controller)
{

	// @todo change this to dynamic routes method, and move to backend plugin
	if(!isset($controller->params['prefix']) || $controller->params['prefix']!='admin' && !defined('CAKEPHP_SHELL'))
	{
		$offlineUrl = '/cms/offline/';
		/*if(cr('cmsConfig.Backend.siteOfflineText'))
		{
			$offlineUrl = cr('cmsConfig.Backend.siteOfflineText');
		}*/
		if(isset($controller->params['url']['url']) && Configure::read('cmsConfig.Backend.siteStatus') == 'off' && strpos('/'.$controller->params['url']['url'], $offlineUrl)===false)
		{
			$controller->redirect($offlineUrl);
		}
	}
}

function CmscopyFilesHook($controller)
{
	// check if base helpers exists in cake setup
	if(!file_exists(LIBS.'view'.DS.'helpers'.DS.'base_form.php'))
	{
		// create base form helper
		_convert_cake_helper('Form');
	}
	
	// check if base helpers exists in cake setup
	if(!file_exists(LIBS.'view'.DS.'helpers'.DS.'base_html.php'))
	{
		// create base form helper
		_convert_cake_helper('Html');
	}
}


function _convert_cake_helper($class) {
	
	$oldClassName = $class.'Helper';
	$newClassName = 'Base'.$class.'Helper';
	
	$newFileName = strtolower(Inflector::underscore('Base'.$class));
	$oldFileName = strtolower(Inflector::underscore($class));
	
	if (class_exists($newClassName)) {
		return;
	}
	$newFile = CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'libs' . DS . 'view' . DS . 'helpers' . DS . $newFileName.'.php';
	$realFile  = CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'libs' . DS . 'view' . DS . 'helpers' . DS . $oldFileName.'.php';

	if(!file_exists($realFile))
		return;
	
	file_put_contents($newFile,
			str_replace(
				'class '.$oldClassName.' extends AppHelper {',
				'class '.$newClassName.' extends AppHelper {',
				file_get_contents($realFile))
	);


	@unlink($realFile);

	// copy inherited helper in app - no need
	//copy(APP.'plugins'.DS.'cms'.DS.'dump'.DS.strtolower($class).'.php', APP.'views'.DS.'helpers'.DS.strtolower($class).'.php');
}


function CmsShellTasksHook($shell)
{
	//$shell->tasks[] = 'DomainNotification';
}


// set available modules here
function CmsGetModulesHook($controller)
{
	$controller->modules['cms'][] = 'mod_htmlBox';
	$controller->modules['cms'][] = 'mod_rss';
}

function CmsBuildAdminMenuHook($backend)
{
	checkSessionKey();
	$backend->addMenuItem(ADMIN_SYSTEM, __('Clear Cache'),array('controller'=>"cms",'plugin'=>"cms",'action'=>'clearCache'));
	$backend->addMenuItem(ADMIN_SYSTEM, __('View Logs'),array('controller'=>"cms",'plugin'=>"cms",'action'=>'logs'));
}

?>